<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>mrdForm</title>
    <jsp:include page="../../head.jsp"/>
</head>
<script type="text/javascript">
    $(function () {
        var companyId = $("#companyId").val();
        if (companyId == "") {
            companyId = "${companyId}";
        }
        $("#mrdForm").mrdForm({
                                  title: "查询列表",// 表单标题
                                  columns: [{
                                      name: "d.name",
                                      label: "部门名称"
                                  }],
                                  buttons: {
                                      'custom': [
                                          {id: "addDept", value: "添加部门", click: add},
                                          {id: "modify", value: "编辑", click: edit},
                                          {id: "delDept", value: "删除", click: deleteDept},
                                          {id: "viewUser", value: "查看人员", click: deptUser}
                                      ],
                                      queryEvent: function () {
                                          var gridSelect = "#" + $.mrdForm.mrdFormSettings.grid.id;
                                          var rules = "";// 定义rules
                                          $.each($("#" + $.mrdForm.formContainerId
                                                   + " td[id^='td_control_']"), function (i, n) {
                                              var control = $(n).children().first();
                                              if (!$.mrdForm.isEmpty(control.val())) {
                                                  rules += "{'field':'" + control.attr('name')
                                                           + "','op':'cn','data':'"
                                                           + control.val() + "'},";// 组装rules
                                              }
                                          });
// 					alert(rules);
                                          // 如果没有查询条件，则返回
                                          if (!$.mrdForm.isEmpty(rules)) {
                                              rules = rules.substring(0, rules.length);
                                              var filters = '{"groupOp":"AND","rules":[' + rules
                                                            + ']}';// 定义filters
                                              var postData = $(gridSelect).jqGrid("getGridParam",
                                                                                  "postData");// 获取将要提交的数据
                                              $.extend(postData, {filters: filters});// 将filters插入将要提交的数据
                                          }
                                          $(gridSelect).jqGrid("setGridParam", {
                                              search: true
                                              // 将jqGrid的search选项设为true
                                          }).trigger("reloadGrid", [{
                                              page: 1
                                          }]); // 重新载入Grid表格
                                      }
                                  },
                                  grid: {
                                      id: "columnGrid",
                                      url: "company-listDept?id=" + companyId,
                                      colNames: ['id', '部门名称', '启用模式', '状态'],
                                      colModel: [{
                                          name: 'id',
                                          index: 'id',
                                          hidden: true,
                                          align: "center"
                                      }, {
                                          name: 'name',
                                          index: 'name',
                                          label: "部门名称",
                                          width: 200,
                                          align: "center"
                                      }, {
                                          name: 'isabled',
                                          index: 'isabled',
                                          label: "启用模式",
                                          width: 70,
                                          align: "center",
                                          //字段值的显示格式转换
                                          formatter: function (cellvalue, options, row) {
                                              if (cellvalue == "1") {
                                                  return "禁用";
                                              } else if (cellvalue == "0") {
                                                  return "启用";
                                              }
                                          }
                                      }, {
                                          name: 'state',
                                          index: 'state',
                                          label: "状态",
                                          width: 50,
                                          align: "center",
                                          //字段值的显示格式转换
                                          formatter: function (cellvalue, options, row) {
                                              if (cellvalue == "1") {
                                                  return "删除";
                                              } else if (cellvalue == "0") {
                                                  return "可用";
                                              }
                                          }
                                      }],
                                      title: "部门列表",
                                      sortname: "ctime",
                                      //rowNum:5,
                                      jsonReader: {
                                          //从哪个json对象中取值
                                          root: "depts"
                                      },
                                      onSelectRow: function (id) {
                                          //parent.document.getElementById("rightFrame").src="../base/company-mainUser?dept.id="+id;
                                      },
                                      ondblClickRow: function (id) {
                                          parent.location.href =
                                              "company-manageDept?dept.id=" + id
                                              + "&company.id=" + $("#companyId").val();
                                      }
                                  }
                              });
        $("#mrdForm").mrdForm("resize");
    });
    //添加
    var add = function () {
        location.href = "../base/company-manageDept?companyId=" + $("#companyId").val();
    };
    var del = function () {
    };
    //编辑
    var edit = function () {
        var selected_rowid = $("#columnGrid").jqGrid("getGridParam", "selrow");
        if (!selected_rowid || "" == selected_rowid || null == selected_rowid) {
            alert("请选择需要编辑的部门");
            return;
        }
        var rowData = $("#columnGrid").getRowData(selected_rowid);
        var deptId = rowData.id;
        location.href =
            "../base/company-manageDept?deptId=" + deptId + "&companyId=" + $(
                "#companyId").val();
    };
    //删除部门
    var deleteDept = function () {
        var selected_rowid = $("#columnGrid").jqGrid("getGridParam", "selrow");
        if (!selected_rowid || "" == selected_rowid || null == selected_rowid) {
            alert("请选择需要删除的部门");
            return;
        }
        var rowData = $("#columnGrid").getRowData(selected_rowid);
        var deptId = rowData.id;
        $.ajax({
                   url: "company-checkIsManageDept",
                   type: "post",
                   dataType: "json",
                   data: "deptId=" + deptId,
                   success: function (data) {
                       if (data == null) {
                           $.ajax({
                                      url: "company-delDept",
                                      dataType: "json",
                                      type: "POST",
                                      data: "deptId=" + deptId,
                                      success: function (data) {
                                          var state = $.parseJSON(data.msg).state;
                                          var msg = $.parseJSON(data.msg).msg;
                                          if (state == 1) {
                                              alert(msg);
                                              location.href = "../base/company-deptList";
                                          }
                                      }
                                  });
                       } else {
                           var state = $.parseJSON(data["data"]).state;
                           if (state == "companyManage") {
                               alert("该部门为管理员部门不能删除!");
                               return;
                           }
                       }
                   }
               });

    }
    //查看部门人员
    var deptUser = function () {
        var selected_rowid = $("#columnGrid").jqGrid("getGridParam", "selrow");
        if (!selected_rowid || "" == selected_rowid || null == selected_rowid) {
            alert("请选择需要编辑的部门");
            return;
        }
        var rowData = $("#columnGrid").getRowData(selected_rowid);
        var deptId = rowData.id;
        $.ajax({
                   url: "company-checkIsManageDept",
                   type: "post",
                   dataType: "json",
                   data: "deptId=" + deptId,
                   success: function (data) {
                       if (data != null && $.parseJSON(data["data"]) != null) {
                           var state = $.parseJSON(data["data"]).state;
                           //alert(state);
                           if (state == "companyManage") {
                               location.href =
                                   "../base/company-mainUser?deptId=" + deptId
                                   + "&state=companyManage";
                           }
                       } else {
                           location.href = "../base/company-mainUser?deptId=" + deptId;
                       }
                   }
               });
    }
</script>
<body>
<input type="hidden" id="companyId" name="company.id" value="${company.getId()}"/>
<div id="mrdForm"></div>
</body>
</html>
